<?php
require_once dirname(__FILE__) . "/../../includes/config.php";
require_once PHP_ROOTPATH.'/classes/database.php';

	function requete($value,$id,$id_connect){
		//récupération des informations de test
		$query_temp1= "SELECT distinct date_test,experimentateur,description
		FROM resultat_test,test,Type_test 
		WHERE description LIKE '%".$value."%'
		AND id_animal=".$id."
		AND resultat_test.id_test=test.id_test
		AND test.id_type_test=type_test.id_type_test
		ORDER BY date_test";
		
		$res_query1 = pg_query($id_connect , $query_temp1) or die(pg_last_error());
		$int = pg_num_rows($res_query1);
		if($value=="Discrimination" || $value=="Inversion" || $value=="Pairwise inversion" || $value=="Pairwise discrimination"){
			if($int!=0){echo "<item id=\"descr\" name=\"" . $value . "\" />\n";}
		}
		while ($back1 = pg_fetch_assoc($res_query1)) {
			if($back1["date_test"]){
				$cpt=0;
				$moy=0;
				if($back1["description"]!="Hourglass wide"){
					echo "<item id=\"date_test\" name=\"" . date("d/m/Y", strtotime($back1["date_test"])) . "\" />\n";
					if($back1["experimentateur"]){echo "<item id=\"Expérimentateur\" name=\"Expérimentateur: " . $back1["experimentateur"] . "\" />\n";}
				}
				
				//récupération des informations dans resultat_test_simple
				$query_temp2 = "SELECT unite_mesure,mesure,resultat_test_simple.description 
				FROM Resultat_test_simple,resultat_test,test,Type_test 
				WHERE type_test.description LIKE '%".$value."%'
				AND id_animal=".$id." AND date_test='".$back1["date_test"]."'
				AND resultat_test.id_test=test.id_test
				AND test.id_type_test=type_test.id_type_test
				AND resultat_test_simple.id_res=resultat_test.id_res";
				
				$res_query2 = pg_query($id_connect , $query_temp2) or die(pg_last_error());
				while ($back2 = pg_fetch_assoc($res_query2)) {
					$cpt++;
					if($value=="Hourglass narrow"){
						$temp=intval($back2["mesure"]);
						$moy+=$temp;
						echo "<item id=\"narrow".date("d/m/Y", strtotime($back1["date_test"]))."\" name=\"" . $value." ".$back2["unite_mesure"] . ": ".$back2["mesure"]."\" />\n";
					}
					elseif($value=="Hourglass wide"){
						$temp=intval($back2["mesure"]);
						$moy+=$temp;
						echo "<item id=\"wide".date("d/m/Y", strtotime($back1["date_test"]))."\" name=\"" . $value." ".$back2["unite_mesure"] . ": ".$back2["mesure"]."\" />\n";
					}
					elseif($value=="Barnes bop"){
						echo "<item id=\"bop".date("d/m/Y", strtotime($back1["date_test"]))."\" name=\"" .$back2["unite_mesure"] . ": ".$back2["mesure"]."\" />\n";
					}
					elseif($value=="Barnes bc12"){
						echo "<item id=\"bc12".date("d/m/Y", strtotime($back1["date_test"]))."\" name=\"" .$back2["unite_mesure"] . ": ".$back2["mesure"]."\" />\n";
					}
					else{
						echo "<item id=\"".$back2["unite_mesure"]."\" name=\"" . $back2["unite_mesure"] . ": ".$back2["mesure"]."\" />\n";
					}
					if($back2["description"]){echo "<item id=\"Description\" name=\"Description: " . $value . "\" />\n";}
				}
				if($moy!=0){
					$moy2=$moy/$cpt;
					if($value=="Hourglass wide"){
						echo "<item id=\"wide".date("d/m/Y", strtotime($back1["date_test"]))."\" name=\"Moyenne ".$value.": ".$moy2."\" />\n";
					}
					else{
						echo "<item id=\"narrow".date("d/m/Y", strtotime($back1["date_test"]))."\" name=\"Moyenne ".$value.": ".$moy2."\" />\n";
					}
				}
			}
		}
		
	}
	
	header("Content-Type: text/xml; charset=utf-8");
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	echo "<list>";
	
	// liste des noms de tests
	$desc = (isset($_POST["description"])) ? html_entity_decode($_POST["description"]) : NULL;
	$id = (isset($_POST["id_anim"])) ? html_entity_decode($_POST["id_anim"]) : NULL;
	
	if($desc && $id){
		$id_connect=Database::getInstance();
		if($desc=="Discrimination/Inversion"){
			$desc1="Discrimination";
			$desc2="Inversion";			
			
			requete($desc1,$id,$id_connect);
			requete($desc2,$id,$id_connect);
		}
		else if($desc=="Pairwise Discrimination/Inversion"){
			$desc1="Pairwise Discrimination";
			$desc2="Pairwise Inversion";
		
			requete($desc1,$id,$id_connect);
			requete($desc2,$id,$id_connect);
		}
		else if($desc=="Hourglass"){
			$desc1="Hourglass narrow";
			$desc2="Hourglass wide";
			
			requete($desc1,$id,$id_connect);
			requete($desc2,$id,$id_connect);
		}
		else if($desc=="Barnes"){
			$desc1="Barnes bc12";
			$desc2="Barnes bop";
			
			requete($desc1,$id,$id_connect);
			requete($desc2,$id,$id_connect);
		}		
		else{		
			requete($desc,$id,$id_connect);
		}
	}

	echo "</list>";

?>
